package com.emex.app.stuff.gui.model;

import com.emex.app.shared.data.Provider;
import com.emex.app.stuff.provider.ProviderDataProvider;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Sergey Karavaev
 */
public class ProviderTableModel extends AbstractTableModel
        implements TableModel, TableCrud<Provider> {

    /**
     * List of all orders.
     */
    private final List<Provider> providers;
    /**
     * Column names.
     */
    protected String[] columnNames = new String[]{
        "Название"
    };
    /**
     * Column classes
     */
    protected Class[] columnClasses = new Class[]{
        String.class
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public Class getColumnClass(int index) {
        return columnClasses[index];
    }

    public ProviderTableModel() {
        providers = ProviderDataProvider.getInstance().getAllProviders();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getRowCount() {
        return providers.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if ((rowIndex >= 0) && (rowIndex < providers.size())
                && (columnIndex >= -1) && (columnIndex < columnNames.length)) {
            switch (columnIndex) {
                case -1: {
                    return providers.get(rowIndex);
                }
                case 0: {
                    return providers.get(rowIndex).getName();
                }
                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public void dataRemoved(Provider t) {
        providers.remove(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataAdded(Provider t) {
        providers.add(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataUpdated() {
        this.fireTableDataChanged();
    }
}